package com.wy.xinzhi.common.actions;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.wy.business.developer.apps.actions.AppSubmitAction;
import com.wy.component.properties.PropertiesUtil;
import com.wy.framework.actions.AbstractBaseAction;
import com.wy.framework.exceptions.ServiceException;
import com.wy.xinzhi.common.pojos.TCustomer;
import com.wy.xinzhi.common.pojos.TFoodRecord;
import com.wy.xinzhi.common.services.CustomerService;
import com.wy.xinzhi.common.services.FoodRecordService;

@ParentPackage("json")
@Namespace(value="/xz")
@SuppressWarnings({"rawtypes","unused"})
public class FoodRecordAction extends AbstractBaseAction {
	private static final long serialVersionUID = 1L;
	private static Log logger = LogFactory.getLog(FoodRecordAction.class);
	private PropertiesUtil p = new PropertiesUtil();

	private List<Map<String,Object>> foodRecList;
	
	private List<Map<String,Object>> foodRecList1;
	
	private List date_data ;
	
	private List kal_data ;
	
	private String jsonRes;
	
	private TCustomer customer;
	
	private TFoodRecord foodRecord;
	//页面参数
	private int page;
	private int pageSize;
	private int total;
	//排序参数
	private String orderCol;
	private String order;
	
	@Autowired
	private FoodRecordService foodRecordService;
	@Autowired
	private CustomerService customerService;
	
	@Action(value = "upIndex", 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
					"false"}) })
	public String upIndex() throws UnsupportedEncodingException, ServiceException{
		String openId=ServletActionContext.getRequest().getParameter("openId");
		String breakfast=ServletActionContext.getRequest().getParameter("breakfast");
		String lunch=ServletActionContext.getRequest().getParameter("lunch");
		String dinner=ServletActionContext.getRequest().getParameter("dinner");
		String add_dinner=ServletActionContext.getRequest().getParameter("add_dinner");
		TFoodRecord record = new TFoodRecord();
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		record.setReId(sdf.format(date)+"-"+openId);
		record.setCustomerId(openId);
		record.setBreakfast(breakfast);
		record.setLunch(lunch);
		record.setDinner(dinner);
		record.setAddDinner(add_dinner);
		this.foodRecordService.saveFoodRec(record);
		return SUCCESS;
	}
	
	@Action(value = "FoodRecChart", 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
					"false"}) })
	public String getFoodRecChart() throws UnsupportedEncodingException, ServiceException{
		String openId=ServletActionContext.getRequest().getParameter("openId");
		String date=ServletActionContext.getRequest().getParameter("date");
		List<Map<String, Object>> resultList = this.foodRecordService.getFoodRecByMonth(openId, date);
		this.foodRecList = resultList;
		this.date_data = new ArrayList();
		this.kal_data = new ArrayList();
		for (int i = 0; i < foodRecList.size(); i++) {
			Map data = foodRecList.get(i);
			date_data.add(data.get("FDAY"));
			kal_data.add(data.get("RES1"));
		}
		return SUCCESS;
	}
	
	@Action(value = "queryFoodRecord", 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
					"false"}) })
	public String queryFoodRecord() throws UnsupportedEncodingException{
			String customerId =ServletActionContext.getRequest().getParameter("openId");
			List<Map<String, Object>> resultList= this.foodRecordService.queryFoodRecord(customerId);
			this.foodRecList1 = resultList;
			customer=this.customerService.loginValid(customerId);
		return SUCCESS;
	}
	
	@Action(value = "getFoodRecById", 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
					"false"}) })
	public String getFoodRecById() throws UnsupportedEncodingException{
			String reId =ServletActionContext.getRequest().getParameter("reId");
			this.foodRecord= this.foodRecordService.getFoodRecordById(reId);
		return SUCCESS;
	}
	
	@Action(value = "saveFoodRec", 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
					"false"}) })
	public String saveFoodRec() throws UnsupportedEncodingException, ServiceException{
		String reId =ServletActionContext.getRequest().getParameter("reId");
		String userId =ServletActionContext.getRequest().getParameter("userId");
		String res1 =ServletActionContext.getRequest().getParameter("res1");
		String feedback =ServletActionContext.getRequest().getParameter("feedback");
		this.foodRecord= this.foodRecordService.getFoodRecordById(reId);
		foodRecord.setUserId(userId);
		foodRecord.setFeedbackDate(new Timestamp(new Date().getTime()));
		foodRecord.setFeedback(feedback);
		foodRecord.setRes1(res1);
		this.foodRecordService.saveFoodRec(foodRecord);
		return SUCCESS;
	}
	
	@Action(value = "deleteFoodRecById", 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
					"false"}) })
	public String deleteFoodRecById() throws UnsupportedEncodingException{
			String reId =ServletActionContext.getRequest().getParameter("reId");
			this.foodRecordService.deleteFoodRecordById(reId);
		return SUCCESS;
	}
	
	public List<Map<String, Object>> getFoodRecList1() {
		return foodRecList1;
	}

	public void setFoodRecList1(List<Map<String, Object>> foodRecList1) {
		this.foodRecList1 = foodRecList1;
	}

	public String getJsonRes() {
		return jsonRes;
	}
	public void setJsonRes(String jsonRes) {
		this.jsonRes = jsonRes;
	}
	public List<Map<String, Object>> getFoodRecList() {
		return foodRecList;
	}

	public void setFoodRecList(List<Map<String, Object>> foodRecList) {
		this.foodRecList = foodRecList;
	}

	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public List getDate_data() {
		return date_data;
	}

	public void setDate_data(List date_data) {
		this.date_data = date_data;
	}

	public List getKal_data() {
		return kal_data;
	}

	public void setKal_data(List kal_data) {
		this.kal_data = kal_data;
	}

	public TCustomer getCustomer() {
		return customer;
	}

	public void setCustomer(TCustomer customer) {
		this.customer = customer;
	}

	public TFoodRecord getFoodRecord() {
		return foodRecord;
	}

	public void setFoodRecord(TFoodRecord foodRecord) {
		this.foodRecord = foodRecord;
	}
}
